package leetcode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-26 13:26
 **/
public class LeetCode515 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root);
        //临时节点
        TreeNode temp = null;
        while (!deque.isEmpty()) {
            int max = Integer.MIN_VALUE;
            //存储当前层的元素个数
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                temp = deque.poll();
                max = Math.max(temp.val, max);
                if (temp.left != null) {
                    deque.offer(temp.left);
                }
                if (temp.right != null) {
                    deque.offer(temp.right);
                }
            }
            list.add(max);
        }
        return list;
    }
}
